package com.onlyxiahui.common.action.description.handler.impl.parameter;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.springframework.core.MethodParameter;

import com.onlyxiahui.common.action.description.DocumentContext;
import com.onlyxiahui.common.action.description.bean.MethodData;
import com.onlyxiahui.common.action.description.bean.ModuleData;
import com.onlyxiahui.common.action.description.bean.ParameterData;
import com.onlyxiahui.common.action.description.enums.annotation.MethodRequest;
import com.onlyxiahui.common.action.description.enums.annotation.ParameterMapping;
import com.onlyxiahui.common.action.description.enums.data.ParameterType;
import com.onlyxiahui.common.action.description.handler.impl.BaseParameterHandler;
import com.onlyxiahui.common.action.description.util.ActionAnnotationUtil;

/**
 * Description <br>
 * Date 2020-01-07 17:24:27<br>
 * 
 * @author XiaHui [onlovexiahui@qq.com]<br>
 * @since 1.0.0
 */

public class FormParameterHandler extends BaseParameterHandler {

	protected String getAnnotation() {
		return ParameterMapping.PathVariable.getAnnotation();
	}

	@Override
	public boolean support(DocumentContext context, ModuleData moduleData, MethodData methodData, Method method, MethodParameter parameter) {

		Annotation[] mas = (null != method) ? method.getAnnotations() : new Annotation[0];
		boolean hasGetMapping = ActionAnnotationUtil.hasAnnotation(MethodRequest.GetMapping.getAnnotation(), mas);
		boolean hasPostMapping = ActionAnnotationUtil.hasAnnotation(MethodRequest.PostMapping.getAnnotation(), mas);
		boolean hasPutMapping = ActionAnnotationUtil.hasAnnotation(MethodRequest.PutMapping.getAnnotation(), mas);
		boolean hasDeleteMapping = ActionAnnotationUtil.hasAnnotation(MethodRequest.DeleteMapping.getAnnotation(), mas);
		boolean hasPatchMapping = ActionAnnotationUtil.hasAnnotation(MethodRequest.PatchMapping.getAnnotation(), mas);
		boolean hasRequestMapping = ActionAnnotationUtil.hasAnnotation(MethodRequest.RequestMapping.getAnnotation(), mas);

		boolean supportMethod = hasRequestMapping || hasGetMapping || hasPostMapping || hasPutMapping || hasDeleteMapping || hasPatchMapping;

		Annotation[] as = (null != parameter) ? parameter.getParameterAnnotations() : null;
		boolean no = null == as || as.length == 0;
		return no && supportMethod;
	}

	@Override
	public ParameterData handle(DocumentContext context, ModuleData moduleData, MethodData methodData, Method method, MethodParameter parameter) {
		String name = "";
		ParameterData pd = handle(context, moduleData, methodData, method, parameter, name);
		pd.setParameterType(ParameterType.item.type());
		return pd;
	}
}
